<template>
    <div class="actionBar" data-id="actionBar">

        <div v-for="item in icons" :draggable="true" class="tag" :data-type="item.type"
            @dragstart="createNode($event, item)">
            <el-tooltip :content="item.label" placement="left">
                <svg-icon class="icon" :name="item.icon"></svg-icon>
            </el-tooltip>

            <span @click="removeItem(item)" v-if="item.type === 'custom'" class="close">
                <el-tooltip content="删除" placement="right">X</el-tooltip>
            </span>

        </div>
    </div>
</template>

<script lang='ts' setup>
import { ElMessage } from 'element-plus';
import { ref } from 'vue';
import { useGraph } from '@/pinia'
import { IconNode } from '@/types/graph';

const GraphStore = useGraph()

let icons = ref<IconNode[]>(GraphStore.icons)

// 创建节点
const createNode = (event: DragEvent, item: IconNode) => {
    event.dataTransfer?.setData('nodeType',
        JSON.stringify(item),
    );
}
const getCombos = () => {

    icons = ref<IconNode[]>(GraphStore.icons)

    if (GraphStore.group.length === 0) return

    GraphStore.group.forEach((item: any) => {
        let d: IconNode = {
            id: item.id,
            label: item.name,
            type: "custom",
            icon: "tiantianquantu",
            data: JSON.parse(item.data),
        }
        icons.value.push(d)
    });
}
const removeItem = (row: IconNode) => {


    GraphStore.removeGroupList(row)

    ElMessage.success(`删除成功`)

    const index = icons.value.findIndex(item => item.label === row.label)
    icons.value.splice(index, 1)
}

defineExpose({
    getCombos
})
getCombos()
</script>

<style lang="less" scoped>
.actionBar {
    background-color: inherit;
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
    padding: 20px 5px;
    box-shadow: 0px 0px 13px 0px var(--shadowColor);
    z-index: 3;
    background-color: transparent;
    max-height: 80%;
    overflow-y: auto;
    border-radius: 5px;

    .tag {
        position: relative;

        &:hover {
            .close {
                display: block;
            }
        }

        .icon {
            padding: 10px;
            width: 60px;
            height: 60px;
            background-color: inherit;
            transition: all .3s;
            border-radius: 8px;
            cursor: pointer;

            &:hover {
                background-color: antiquewhite;
            }
        }

        .close {
            display: none;
            position: absolute;
            right: 0px;
            top: 0px;
            cursor: pointer;

            &:hover {
                color: #049eff;
            }
        }
    }
}
</style>